package com.sqsf.servlet;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "TestServlet", value = "/test")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取全局作用域servletContext对象
        ServletContext servletContext = request.getServletContext();
        //通过spring提供的工具类WebApplicationContextUtils
        //调用其静态方法getWebApplicationContext
        //传入全局作用域对象servletContext
        //会自动返回spring容器对象
        WebApplicationContext spring = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        //调用spring容器对象方法获取对象进行使用
        System.out.println(spring.getBean("user"));
    }
}